/**
 * Copyright (c) 2009  Collaborative Development Group, C.S. Dept., University of Bari
 *
 * All rights reserved. This program and the accompanying materials are made available under
 * the terms of the Eclipse Public License v1.0  which accompanies this distribution, and is 
 * available at http://www.eclipse.org/legal/epl-v10.html
 */
package it.uniba.di.cdg.penelope.ui.lnf;

import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.forms.widgets.Form;
import org.eclipse.ui.forms.widgets.FormToolkit;

import com.google.inject.Inject;
import com.google.inject.Singleton;


/**
 * Implementation using the {@link FormToolkit} for the application theme. 
 */
@Singleton
public class FormToolkitWidgetFactory implements IWidgetFactory {

	private final Display display;
	private final FormToolkit toolkit; 
	
	private Color backgroundColor = null;

	
	@Inject
	public FormToolkitWidgetFactory( Display display ) {
		this.display = display;
		this.toolkit = new FormToolkit( display );
	}	
		
	/* (non-Javadoc)
	 * @see it.uniba.di.cdg.jtalk.ui.ITheme#getBackgroundColor()
	 */
	@Override
	public Color getCompositeBackgroundColor() {
		if (backgroundColor == null) {
			backgroundColor = new Color( display, 255, 255, 255 ); // White
		}
		return backgroundColor;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.ui.services.IDisposable#dispose()
	 */
	@Override
	public void dispose() {
		if (backgroundColor != null) {
			backgroundColor.dispose();
		}
		toolkit.dispose();
	}

	/* (non-Javadoc)
	 * @see it.uniba.di.cdg.jtalk.ui.IWidgetFactory#applyBackgroundColor(org.eclipse.swt.widgets.Composite)
	 */
	@Override
	public void applyBackgroundColor( Composite composite ) {
		composite.setBackground( getCompositeBackgroundColor() );
	}

	/* (non-Javadoc)
	 * @see it.uniba.di.cdg.jtalk.ui.IWidgetFactory#newForm(org.eclipse.swt.widgets.Composite)
	 */
	@Override
	public Form newForm( Composite parent ) {
		return toolkit.createForm( parent );
	}
}
